﻿// Type: System.Web.Mvc.ControllerContext
// Assembly: System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

using System;
using System.Web;
using System.Web.Routing;

namespace System.Web.Mvc
{
  /// <summary>
  /// Encapsulates information about an HTTP request that matches specified <see cref="T:System.Web.Routing.RouteBase"/> and <see cref="T:System.Web.Mvc.ControllerBase"/> instances.
  /// </summary>
  public class ControllerContext
  {
    internal const string PARENT_ACTION_VIEWCONTEXT = "ParentActionViewContext";
    private HttpContextBase _httpContext;
    private RequestContext _requestContext;
    private RouteData _routeData;

    /// <summary>
    /// Gets or sets the controller.
    /// </summary>
    /// 
    /// <returns>
    /// The controller.
    /// </returns>
    public virtual ControllerBase Controller { get; set; }

    /// <summary>
    /// Gets or sets the HTTP context.
    /// </summary>
    /// 
    /// <returns>
    /// The HTTP context.
    /// </returns>
    public virtual HttpContextBase HttpContext
    {
      get
      {
        if (this._httpContext == null)
          this._httpContext = this._requestContext != null ? this._requestContext.HttpContext : (HttpContextBase) new ControllerContext.EmptyHttpContext();
        return this._httpContext;
      }
      set
      {
        this._httpContext = value;
      }
    }

    /// <summary>
    /// Gets a value that indicates whether the associated action method is a child action.
    /// </summary>
    /// 
    /// <returns>
    /// true if the associated action method is a child action; otherwise, false.
    /// </returns>
    public virtual bool IsChildAction
    {
      get
      {
        RouteData routeData = this.RouteData;
        if (routeData == null)
          return false;
        else
          return routeData.DataTokens.ContainsKey("ParentActionViewContext");
      }
    }

    /// <summary>
    /// Gets an object that contains the view context information for the parent action method.
    /// </summary>
    /// 
    /// <returns>
    /// An object that contains the view context information for the parent action method.
    /// </returns>
    public ViewContext ParentActionViewContext
    {
      get
      {
        return this.RouteData.DataTokens["ParentActionViewContext"] as ViewContext;
      }
    }

    /// <summary>
    /// Gets or sets the request context.
    /// </summary>
    /// 
    /// <returns>
    /// The request context.
    /// </returns>
    public RequestContext RequestContext
    {
      get
      {
        if (this._requestContext == null)
          this._requestContext = new RequestContext(this.HttpContext ?? (HttpContextBase) new ControllerContext.EmptyHttpContext(), this.RouteData ?? new RouteData());
        return this._requestContext;
      }
      set
      {
        this._requestContext = value;
      }
    }

    /// <summary>
    /// Gets or sets the URL route data.
    /// </summary>
    /// 
    /// <returns>
    /// The URL route data.
    /// </returns>
    public virtual RouteData RouteData
    {
      get
      {
        if (this._routeData == null)
          this._routeData = this._requestContext != null ? this._requestContext.RouteData : new RouteData();
        return this._routeData;
      }
      set
      {
        this._routeData = value;
      }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Mvc.ControllerContext"/> class.
    /// </summary>
    public ControllerContext()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Mvc.ControllerContext"/> class by using the specified controller context.
    /// </summary>
    /// <param name="controllerContext">The controller context.</param><exception cref="T:System.ArgumentNullException">The <paramref name="controllerContext"/> parameter is null.</exception>
    protected ControllerContext(ControllerContext controllerContext)
    {
      if (controllerContext == null)
        throw new ArgumentNullException("controllerContext");
      this.Controller = controllerContext.Controller;
      this.RequestContext = controllerContext.RequestContext;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Mvc.ControllerContext"/> class by using the specified HTTP context, URL route data, and controller.
    /// </summary>
    /// <param name="httpContext">The HTTP context.</param><param name="routeData">The route data.</param><param name="controller">The controller.</param>
    public ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller)
      : this(new RequestContext(httpContext, routeData), controller)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:System.Web.Mvc.ControllerContext"/> class by using the specified request context and controller.
    /// </summary>
    /// <param name="requestContext">The request context.</param><param name="controller">The controller.</param><exception cref="T:System.ArgumentNullException">One or both parameters are null.</exception>
    public ControllerContext(RequestContext requestContext, ControllerBase controller)
    {
      if (requestContext == null)
        throw new ArgumentNullException("requestContext");
      if (controller == null)
        throw new ArgumentNullException("controller");
      this.RequestContext = requestContext;
      this.Controller = controller;
    }

    private sealed class EmptyHttpContext : HttpContextBase
    {
    }
  }
}
